package hk.m4s.lr.repair.test.ui.goods;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import framework.common.Constant;
import framework.common.baseui.UeBaseActivity;
import framework.common.zanetwork.core.ResponseCallback;
import framework.utils.SharedPreferencesUtils;
import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.GoodsCarModel;
import hk.m4s.lr.repair.test.ui.adapter.GoodsCarAdapter;
import hk.m4s.lr.repair.test.views.listview.VListView;
import okhttp3.Response;

/**
 * Created by qi.wang on 2017/5/11.
 * 购物车
 */

public class GoodsCarActivity extends UeBaseActivity {
    private VListView vlist;
    private GoodsCarAdapter goodsCarAdapter;
    private Context context;
    private TextView selct_buy,selct_all,selct_price;
    private int pageNum = 1;
    private int pageSize = 10;
    private int selectAll=1;
    private int checkNum;
    private LinearLayout goodsTips,bottom_layout;
    public  List<GoodsCarModel.ListBean>shopModelList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseSetContentView(R.layout.activity_goods_car);
        hiddenFooter();
        showGoBackBtn();
        context=this;
        setTitleText("购物车");
        findBaseView();

        findShopingByUser();
    }
    public void findBaseView(){
        vlist=(VListView)findViewById(R.id.vlist);
        selct_buy=(TextView) findViewById(R.id.selct_buy);
        selct_all=(TextView) findViewById(R.id.selct_all);
        selct_price=(TextView) findViewById(R.id.selct_price);
        goodsTips=(LinearLayout)findViewById(R.id.goodsTips);
        bottom_layout=(LinearLayout)findViewById(R.id.bottom_layout);
        goodsCarAdapter=new GoodsCarAdapter(GoodsCarActivity.this,shopModelList);
        vlist.setAdapter(goodsCarAdapter);

    }


    @Override
    protected void onResume() {
        super.onResume();
        // getData("1");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    public void onClick(View v){
        switch (v.getId()){
            case R.id.selct_all:
//                   if(shopModelList.size()>0) {
//                       if (selectAll == 1) {
//                           List<GoodsCarModel.ListBean> shopModelLists = new ArrayList<>();
//                           selectAll = 0;
//                           Drawable drawable = getResources().getDrawable(R.mipmap.un_login);
//                           drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
//                           selct_all.setCompoundDrawables(drawable, null, null, null);
//                           selct_price.setText("¥0.0");
//                           for (int i = 0; i < shopModelList.size(); i++) {
//                               GoodsCarModel.ListBean model = shopModelList.get(i);
//                               model.setSelect("2");
//                               shopModelLists.add(model);
//                           }
//                           goodsCarAdapter.list = shopModelLists;
//                           goodsCarAdapter.notifyDataSetChanged();
//                       } else {
//                           List<GoodsCarModel.ListBean> shopModelLists = new ArrayList<>();
//                           selectAll = 1;
//                           Drawable drawable = getResources().getDrawable(R.mipmap.anto_login);
//                           drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
//                           selct_all.setCompoundDrawables(drawable, null, null, null);
//                           for (int i = 0; i < shopModelList.size(); i++) {
//                               GoodsCarModel.ListBean model = shopModelList.get(i);
//                               model.setSelect("1");
//                               shopModelLists.add(model);
//                           }
//                           goodsCarAdapter.list = shopModelLists;
//                           goodsCarAdapter.notifyDataSetChanged();
//                           Double count = 0.0;
//                           if (goodsCarAdapter != null) {
//                               if (goodsCarAdapter.list != null) {
//                                   for (GoodsCarModel.ListBean been : goodsCarAdapter.list) {
//                                       if (!been.getGoods_price().equals("")) {
//                                           count = count
//                                                   + (Integer.parseInt(been.getGoods_num()) * (Double
//                                                   .parseDouble(been.getGoods_price())));
//                                       }
//                                   }
//                                   selct_price.setText(MoneyTool.getLocalMoney(count+""));
//                               }
//                           }
//                       }
//                       List<GoodsCarModel.ListBean> lists = new ArrayList<GoodsCarModel.ListBean>();
//                       for (GoodsCarModel.ListBean beens : goodsCarAdapter.list) {
//                           if (beens.getSelect().equals("1")) {
//                               lists.add(beens);
//                           }
//                       }
//                       selct_buy.setText("去结算("+lists.size()+")");
//                   }
                break;
            case R.id.selct_buy:
//                if(shopModelList.size()>0) {
//                    List<GoodsCarModel.ListBean> lists = new ArrayList<GoodsCarModel.ListBean>();
//                    for (GoodsCarModel.ListBean been : goodsCarAdapter.list) {
//                        if (been.getSelect().equals("1")) {
//                            lists.add(been);
//                        }
//                    }
//                    Intent intent = new Intent(GoodsCarActivity.this, OrderSureActivity.class);
//                    intent.putExtra("type", "2");
//                    intent.putExtra("shopList", (Serializable) lists);
//                    startActivity(intent);
//                }
                break;
        }
    }
    public void checkAll(int counts,int selectnum){
        if(checkNum==counts){
            selectAll = 1;
            Drawable drawable = getResources().getDrawable(R.mipmap.couponcheckd);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            selct_all.setCompoundDrawables(drawable, null, null, null);

        }else{
            selectAll = 0;
            Drawable drawable = getResources().getDrawable(R.mipmap.couponcheck);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            selct_all.setCompoundDrawables(drawable, null, null, null);
        }
//        GoodsCarModel.ListBean been = goodsCarAdapter.list.get(selectnum);
//        if (!(been.getGoods_price() == null || been.getGoods_price().equals(""))) {
//            Double
//                    price=0.0;
//            ;
//            if (selct_price.getText().toString() != null) {
//
//                for(GoodsCarModel.ListBean goods:goodsCarAdapter.list){
//                    if(goods.getSelect().equals("1")){
//                        price=price
//                                + (Integer.parseInt(goods
//                                .getGoods_num()) * (Double
//                                .parseDouble(goods
//                                        .getGoods_price())));
//                    }
//                }
//                selct_price.setText(MoneyTool.getLocalMoney(price+""));
//            }
//        }
//        List<GoodsCarModel.ListBean> lists = new ArrayList<GoodsCarModel.ListBean>();
//        for (GoodsCarModel.ListBean beens : goodsCarAdapter.list) {
//            if (beens.getSelect().equals("1")) {
//                lists.add(beens);
//            }
//        }
       // selct_buy.setText("去结算("+lists.size()+")");
    }
    /**
     * 刷新界面 总价、购买数量等
     * @param
     */
    public  void update(){
//        if(goodsCarAdapter!=null) {
//            Double count = 0.0;
//            if (goodsCarAdapter.list != null) {
//                if(goodsCarAdapter.list.size()>0) {
//                    goodsTips.setVisibility(View.GONE);
//                    for (GoodsCarModel.ListBean been : goodsCarAdapter.list) {
//                        if (been.getSelect().equals("1")) {
//                            if(been.getGoods_price()!=null) {
//                                if (!been.getGoods_price().equals("")) {
//                                    count = count
//                                            + (Integer.parseInt(been.getGoods_num()) * (Double
//                                            .parseDouble(been.getGoods_price())));
//                                }
//                            }
//                        }
//                    }
//                    selct_price.setText(MoneyTool.getLocalMoney(count+""));
//                }else{
//                    selct_price.setText("¥ 0.0");
//                    goodsTips.setVisibility(View.VISIBLE);
//                }
//            }else{
//                goodsTips.setVisibility(View.VISIBLE);
//            }
//        }
    }
    public static Double roundDouble(double val, int precision) {
        Double ret = null;
        try {
            double factor = Math.pow(10, precision);
            ret = Math.floor(val * factor + 0.5) / factor;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    }

    public void deleteGoods(final GoodsCarModel.ListBean shoping) {
//        Map<String, Object> parmas = new HashMap<>();
//        parmas.put("device_type", Constant.device_type);
//        parmas.put("userId", SharedPreferencesUtils.getSharedPreferences(Constant.userId, ""));
//        parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
//        parmas.put("shopingId",shoping.getId());
//        AccountSerive.deleteShopingGoods(context, parmas, new ResponseCallback<GoodsCarModel>() {
//            @Override
//            public void onSuccess(GoodsCarModel carModel) {
//                shopModelList.remove(shoping);
//                setTitleText("购物车("+shopModelList.size()+")");
//                List<GoodsCarModel.ListBean> lists = new ArrayList<GoodsCarModel.ListBean>();
//                for (GoodsCarModel.ListBean beens : shopModelList) {
//                    if (beens.getSelect().equals("1")) {
//                        lists.add(beens);
//                    }
//                }
//                selct_buy.setText("去结算("+lists.size()+")");
//                update();
//                goodsCarAdapter.notifyDataSetChanged();
//            }
//
//            @Override
//            public void onError(Response response, int errorCode, String errorMsg, Exception e) {
//
//            }
//        });

    }
    public void updateGoods(final GoodsCarModel.ListBean shoping) {
        Intent in = new Intent(GoodsCarActivity.this, SlectGoodsSignActivity.class);
        in.putExtra("gocar", "3");
        in.putExtra("goods_id", shoping.getGoods_id());
        in.putExtra("goodsCount", shoping.getGoods_num()+"");
        in.putExtra("goodsUrl", shoping.getImg());
        in.putExtra("goodsName",shoping.getGoods_name());
        in.putExtra("shopId",shoping.getShop_id());
        in.putExtra("goodsPrice", shoping.getGoods_price());
        in.putExtra("detailsId", shoping.getShoping_details_id());
        in.putExtra("goodsSize", shoping.getProperty());
        startActivityForResult(in, 10);
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {

        } else {
            switch (requestCode) {
                case 10: // 我的收货地址
                    findShopingByUser();
                    break;
                default:
                    break;
            }
        }


    }
    public void findShopingByUser() {//获取购物车数量
//        Map<String, Object> parmas = new HashMap<>();
//        parmas.put("device_type", Constant.device_type);
//        parmas.put("userId", SharedPreferencesUtils.getSharedPreferences(Constant.userId, ""));
//        parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
//        AccountSerive.findShopingByUser(context, parmas, new ResponseCallback<GoodsCarModel>() {
//            @Override
//            public void onSuccess(GoodsCarModel carModel) {
//                shopModelList.clear();
//                if(carModel!=null){
//                    bottom_layout.setVisibility(View.VISIBLE);
//                    goodsTips.setVisibility(View.GONE);
//                        if (carModel.getList() != null) {
//                            shopModelList.addAll(carModel.getList());
//                        }else{
//                            goodsTips.setVisibility(View.VISIBLE);
//                        }
//                }else{
//                    bottom_layout.setVisibility(View.GONE);
//                    goodsTips.setVisibility(View.VISIBLE);
//                }
//                goodsCarAdapter.notifyDataSetChanged();
//                checkNum=shopModelList.size();
//                List<GoodsCarModel.ListBean> lists = new ArrayList<GoodsCarModel.ListBean>();
//                for (GoodsCarModel.ListBean beens : shopModelList) {
//                    if (beens.getSelect().equals("1")) {
//                        lists.add(beens);
//                    }
//                }
//                setTitleText("购物车("+shopModelList.size()+")");
//                selct_buy.setText("去结算("+lists.size()+")");
//                update();
//                vlist.setPullRefreshEnable(false);
//                vlist.setPullLoadEnable(false);
//            }
//
//            @Override
//            public void onError(Response response, int errorCode, String errorMsg, Exception e) {
//
//            }
//        });

    }
}
